﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace AramaruForest
{
    public partial class AramaruForest : Form
    {
        private GameState _gameState;
        private Stopwatch _timer = new Stopwatch();
        private double _lastTime;
        private long _frameCounter;
        public Bitmap _bigPicture;
        public Graphics _G;
        


        public AramaruForest()
        {
            InitializeComponent();
            //this.SetStyle(

            //ControlStyles.AllPaintingInWmPaint |

            //ControlStyles.UserPaint |

            //ControlStyles.DoubleBuffer, true);

            _gameState = new GameState(ClientSize);
            initialize();
        }

        private void initialize()
        {
            _gameState.ReadMessages();

            _gameState.Initialize();
            //Initialise and start the timer
            _lastTime = DateTime.Now.TimeOfDay.TotalMilliseconds / 1000;

        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            // We don't have a background to paint, so do nothing here...

            //base.OnPaintBackground(e);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (Width > 0 && Height > 0)
            {
                if (_G != null)
                    _G.Dispose();
                if (_bigPicture != null)
                    _bigPicture.Dispose();
                _bigPicture = new Bitmap(Width, Height);
                _G = Graphics.FromImage(_bigPicture);
            }
        }

        protected override void OnPaint(PaintEventArgs e) // <<==============
        {
            												 // |
            //Work out how long since we were last here in seconds		 // |
            //double gameTime = _timer.ElapsedMilliseconds / 1000.0;	     // |
            //double elapsedTime = gameTime - _lastTime;					 // |
            //_lastTime = gameTime;										 // |
            //_frameCounter++;	

            double gameTime = DateTime.Now.TimeOfDay.TotalMilliseconds / 1000;
            double elapsedTime = gameTime - _lastTime;
            _lastTime = gameTime;
            _G.Clear(Color.Black);
            _frameCounter++;// |
                                                                         // |	This is called the "Game loop"
            //Perform any animation and updates							 // | 
            _gameState.Update(gameTime, elapsedTime);					 // |   In a Windows game, this is driven by the 
                                                                         // |   messaging loop, which every Windows (user) 
            //Draw everything											 // |   program has. By calling Invalidate() at the
            _gameState.Draw(_G);
            e.Graphics.DrawImage(_bigPicture, new Point(0, 0));								 // |   end, we post a "paint me" message to the program
                                                                         // |   message queue, which gets picked up and eventually
            //Force the next Paint()                                     // |   makes this method run again.
            this.Invalidate();
                                 // >===============================
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            _gameState.KeyDown(e.KeyCode);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (!_gameState._world._gameItems.Hero.thrown)
            {
                if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                    _gameState._world._gameItems.Hero.Velocity.X = 0;
                //if (e.KeyCode == Keys.Up && _gameState._world._gameItems.Hero.Velocity.Y < 0)
                //    _gameState._world._gameItems.Hero.JumpForce = 0;
            }
        }
        
    }
}
